package cn.lf.android.lawfirm.inputsource.conntroller;

import android.content.Context;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.inputsource.model.People;
import cn.lf.android.lawfirm.inputsource.model.SourcePeople;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

/**
 * 安源人controller
 * Created by Administrator on 2017/8/7.
 */

public class SourcePeopleController {

    private SourcePeopleCallback mSourcePeopleCallback;
    private Context context;

    public SourcePeopleController(SourcePeopleCallback SourcePeopleCallback, Context context) {
        this.mSourcePeopleCallback = SourcePeopleCallback;
        this.context = context;
    }

    public void onSourcePeople(String isBusiness) {
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("Authorization", spUtil.getAccessToken());
        if (isBusiness.length() > 0) {
            params.put("isBusiness", isBusiness);
        }
        DebugLog.e("333===");
        DebugLog.e("isBusiness===" + isBusiness);
        new MyOkHttpClient().doGet(AppConfig.LSYSUSERS, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("333===" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                arraylist = myJsonObject.getJSONArray("responsObject")
                                        .toString();
                                List<SourcePeople> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<SourcePeople>>() {
                                                });
                                List<People> beans2 = new ArrayList<People>();
                                for (SourcePeople sourcePeople : beans) {
                                    beans2.addAll(sourcePeople.userList);
                                }
                                DebugLog.e("333=beans2==" + beans2);
                                mSourcePeopleCallback.onSourcePeopleSuccessed(beans2);
                            } else {
                                mSourcePeopleCallback
                                        .onSourcePeopleFailed("");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mSourcePeopleCallback.onSourcePeopleFailed(errorMsg);
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public interface SourcePeopleCallback {
        public void onSourcePeopleSuccessed(List<People> bean);

        public void onSourcePeopleFailed(String errorMsg);
    }

}
